﻿/// <reference path="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\MicrosoftAjax.js" />
/// <reference path="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\SP.Core.debug.js" />
/// <reference path="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\SP.debug.js" />

Type.registerNamespace("Pdogs.Air");

Pdogs.Air.Utils = function () {
	var _errorselector = '',
	set_errorSelector = function (s) {
		_errorselector = s;
	},
	get_errorSelector = function () {
		return _errorselector;
	},
	clearError = function (selector) {
		var s = (typeof (selector) === 'undefined' || selector == null || selector === '')
					? _errorselector
					: selector;
		if (s == null || s === '') return;
		$(s).empty();
	},
	onJsomError = function (sender, args) {
		jsomException(_errorselector, args);
	},
	onAjaxError = function (error, requestUri) {
		ajaxException(_errorselector, error, requestUri);
	},
	jsomException = function (selector, e, message, append) {
		var m = "";
		if (typeof (append) === 'undefined') append = false;
		if (typeof (message) === 'undefined' || message === "") {
			m = "An error occurred";
		} else {
			m = message;
		}
		m = m + ": " + e.get_message();

		var msg = '';
		msg = msg + "<p style='margin:0 0;padding-left:5px;'>&raquo;&nbsp;Error Code: " + e.get_errorCode() + "</p>";
		msg = msg + "<p style='margin:0 0;padding-left:5px;'>&raquo;&nbsp;Details: " + e.get_errorDetails() + "</p>";
		msg = msg + "<p style='margin:0 0;padding-left:5px;'>&raquo;&nbsp;Error Type: " + e.get_errorTypeName() + "</p>";
		msg = msg + "<p style='margin:0 0;padding-left:5px;'>&raquo;&nbsp;Error Value: " + e.get_errorValue() + "</p>";
		msg = msg + "<p style='margin:0 0;padding-left:5px;'>&raquo;&nbsp;Stack Trace: " + e.get_stackTrace() + "</p>";

		var h = $("<div style='padding:0px 0.7em; margin-top:10px; width:50%;' class='ui-state-error'>" +
					"<p>" +
						"<span style='margin-right:0.3em; float:left; margin-top:0px;' class='ui-icon ui-icon-alert'/>" +
						"<h3>" + m + "</h3>" +
						msg +
					"</p>" +
				"</div>");
		var s = (typeof (selector) === 'undefined' || selector == null || selector === '')
					? _errorselector
					: selector;
		if (s == null || s === '') {
			alert(h);
			return;
		}
		if (append) {
			$(s).append(h);
		} else {
			$(s).html(h);
		}
	},
	ajaxException = function (selector, e, message, append) {
		var m = "";
		if (typeof (append) === 'undefined') append = false;
		if (typeof (message) === 'undefined' || message === "") {
			m = "An error occurred";
		} else {
			m = message;
		}
		m = "Ajax Request: <span style='word-break:break-all;font-size:0.8em;'>" + m + "</span>";

		var msg = '';
		try {
			msg = msg + "<p style='margin:0 0;padding-left:5px;font-size:0.8em;'>&raquo;&nbsp;Ready State: " + e.readyState + "</p>";
			msg = msg + "<p style='margin:0 0;padding-left:5px;font-size:0.8em;'>&raquo;&nbsp;Status: " + e.status + "</p>";
			msg = msg + "<p style='margin:0 0;padding-left:5px;font-size:0.8em;'>&raquo;&nbsp;StatusText: " + e.statusText + "</p>";
			var o = $.parseJSON(e.responseText);
			msg = msg + "<p style='margin:0 0;padding-left:5px;font-size:0.8em;'>&raquo;&nbsp;Error Code: " + o.error.code + "</p>";
			msg = msg + "<p style='margin:0 0;padding-left:5px;font-size:0.8em;'>&raquo;&nbsp;Message: " + o.error.message.value + "</p>";
		}
		catch (e) {
			msg = "<p style='margin:0 0;padding-left:5px;font-size:0.8em;'>" + $.stringify(e) + "</p>";
		}
		var h = $("<div style='padding:0px 0.7em; margin-top:10px; width:98%;' class='ui-state-error'>" +
					"<p>" +
						"<span style='margin-right:0.3em; float:left; margin-top:0px;' class='ui-icon ui-icon-alert'/>" +
						"<h3 style='word-break:break-all;font-size:0.95em;'>" + m + "</h3>" +
						msg +
					"</p>" +
				"</div>");
		var s = (typeof (selector) === 'undefined' || selector == null || selector === '')
					? _errorselector
					: selector;
		if (s == null || s === '') {
			alert(h);
			return;
		}
		if (append) {
			$(s).append(h);
		} else {
			$(s).html(h);
		}
	},
	userException = function (selector, e, message, append) {
		var m = "";
		if (typeof (append) === 'undefined') append = false;
		if (typeof (message) === 'undefined' || message === "") {
			m = "An error occurred";
		} else {
			m = message;
		}
		m = m + ": " + e.message;

		var msg = '';
		for (var p in e) {
			if ((typeof (e[p]) === 'function') || (p === 'message')) continue;
			msg = msg + "<p style='margin:0 0;padding-left:5px;'>&raquo;&nbsp;" + p + ": " + e[p] + "</p>";
		}
		var h = $("<div style='padding:0px 0.7em; margin-top:10px; width:50%;' class='ui-state-error'>" +
					"<p>" +
						"<span style='margin-right:0.3em; float:left; margin-top:0px;' class='ui-icon ui-icon-alert'/>" +
						"<h3>" + m + "</h3>" +
						msg +
					"</p>" +
				"</div>");
		var s = (typeof (selector) === 'undefined' || selector == null || selector === '')
					? _errorselector
					: selector;
		if (s == null || s === '') {
			alert(h);
			return;
		}
		if (append) {
			$(s).append(h);
		} else {
			$(s).html(h);
		}
	},
	parseXml = function (xml) {
		var dom = null;
		if (window.DOMParser) {
			try {
				dom = (new DOMParser()).parseFromString(xml, "text/xml");
			}
			catch (e)
			{ dom = null; }
		}
		else if (window.ActiveXObject) {
			try {
				dom = new ActiveXObject('Microsoft.XMLDOM');
				dom.async = false;
				if (!dom.loadXML(xml)) // parse error ..
					window.alert(dom.parseError.reason + dom.parseError.srcText);
			}
			catch (e)
			{ dom = null; }
		}
		else
			window.alert("cannot parse xml string!");
		return dom;
	},
	xmlToJson = function (xml) {
		var obj = {};
		if (xml.nodeType == 1) {
			if (xml.attributes.length > 0) {
				obj["@attributes"] = {};
				for (var j = 0; j < xml.attributes.length; j++) {
					var attribute = xml.attributes.item(j);
					obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
				}
			}
		} else if (xml.nodeType == 3) {
			obj = xml.nodeValue;
		}
		if (xml.hasChildNodes()) {
			for (var i = 0; i < xml.childNodes.length; i++) {
				var item = xml.childNodes.item(i);
				var nodeName = item.nodeName;
				if (typeof (obj[nodeName]) == "undefined") {
					obj[nodeName] = xmlToJson(item);
				} else {
					if (typeof (obj[nodeName].push) == "undefined") {
						var old = obj[nodeName];
						obj[nodeName] = [];
						obj[nodeName].push(old);
					}
					obj[nodeName].push(xmlToJson(item));
				}
			}
		}
		return obj;
	};

	return {
		xmlToJson: xmlToJson,
		parseXml: parseXml,
		onJsomError: onJsomError,
		onAjaxError: onAjaxError,
		jsomException: jsomException,
		ajaxException: ajaxException,
		userException: userException,
		get_errorSelector: get_errorSelector,
		set_errorSelector: set_errorSelector,
		clearError: clearError
	};
}();

(function ($) {
	/* String Extensions */
	String.prototype.trimEndsWith = function (suffix) {
		return this.endsWith(suffix)
			? this.substring(0, this.length - suffix.length)
			: this;
	};

	String.prototype.trimEnding = function (suffix) {
		var s = this;
		while ((s.length > 0) && s.endsWith(suffix)) {
			s = s.substring(0, s.length - suffix.length);
		}
		return s;
	};

	String.prototype.trimStartsWith = function (prefix) {
		return this.startsWith(prefix)
			? this.substring(prefix.length)
			: this;
	};

	String.prototype.trimStarting = function (prefix) {
		var s = this;
		while ((s.length > 0) && s.startsWith(prefix)) {
			s = s.substring(prefix.length);
		}
		return s;
	};

	$.extend({
		stringify: function stringify(obj) {
			if (window.JSON && window.JSON.stringify) {
				return window.JSON.stringify(obj);
			}

			var t = typeof (obj);
			if (t != "object" || obj === null) {
				// simple data type
				if (t == "string") obj = '"' + obj + '"';

				return String(obj);
			} else {
				// recurse array or object
				var n, v, json = [], arr = (obj && obj.constructor == Array);

				for (n in obj) {
					v = obj[n];
					t = typeof (v);
					if (obj.hasOwnProperty(n)) {
						if (t == "string") {
							v = '"' + v + '"';
						} else if (t == "object" && v !== null) {
							v = jQuery.stringify(v);
						}

						json.push((arr ? "" : '"' + n + '":') + String(v));
					}
				}

				return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
			}
		}
	});
}(jQuery));

SP.SOD.notifyScriptLoadedAndExecuteWaitingJobs("air.js");
